package org.jeecg.modules.veterans.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.util.SecurityUtil;
import org.jeecg.modules.veterans.entity.SysViewRecord;
import org.jeecg.modules.veterans.service.ISysViewRecordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 浏览记录信息表
 * @Author: jeecg-boot
 * @Date:   2022-07-06
 * @Version: V1.0
 */
@Api(tags="浏览记录信息表")
@RestController
@RequestMapping("/veterans/sysViewRecord")
@Slf4j
public class SysViewRecordController extends JeecgController<SysViewRecord, ISysViewRecordService> {
	@Autowired
	private ISysViewRecordService sysViewRecordService;
	
	/**
	 * 分页列表查询
	 *
	 * @param sysViewRecord
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "浏览记录信息表-分页列表查询")
	@ApiOperation(value="浏览记录信息表-分页列表查询", notes="浏览记录信息表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<SysViewRecord>> queryPageList(SysViewRecord sysViewRecord,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SysViewRecord> queryWrapper = QueryGenerator.initQueryWrapper(sysViewRecord, req.getParameterMap());
		Page<SysViewRecord> page = new Page<SysViewRecord>(pageNo, pageSize);
		IPage<SysViewRecord> pageList = sysViewRecordService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 获取业务浏览总数
	  *
	  * @param recordId
	  * * @param type
	  * @return
	  */
	 //@AutoLog(value = "浏览记录信息表-获取业务浏览总数")
	 @ApiOperation(value="浏览记录信息表-获取业务浏览总数", notes="浏览记录信息表-获取业务浏览总数")
	 @GetMapping(value = "/total")
	 public Result<String> total(@RequestParam(name = "recordId", required = true) String recordId,@RequestParam(name = "type", required = true) String type) {
	 	String total = "0";
		 LoginUser loginUser = SecurityUtil.getLoginUser();
		 QueryWrapper<SysViewRecord> queryWrapper = new QueryWrapper<>();
//		 queryWrapper.eq("type",type);
		 queryWrapper.eq("record_id",recordId);
//		 queryWrapper.ne("user_id",loginUser.getId());
		 queryWrapper.eq("view","1");
		 queryWrapper.select("distinct user_id,record_id");
		 List<SysViewRecord> list = sysViewRecordService.list(queryWrapper);
		 if(!list.isEmpty()){
			 total = String.valueOf(list.size());
		 }

		 return Result.OK(total);
	 }



	
	/**queryWrapper.select("DISTINCT no,name").orderByAsc("no");
	 *   添加
	 *
	 * @param sysViewRecord
	 * @return
	 */
	@AutoLog(value = "浏览记录信息表-添加")
	@ApiOperation(value="浏览记录信息表-添加", notes="浏览记录信息表-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody SysViewRecord sysViewRecord) {
		sysViewRecordService.saveByOne(sysViewRecord);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param sysViewRecord
	 * @return
	 */
	@AutoLog(value = "浏览记录信息表-编辑")
	@ApiOperation(value="浏览记录信息表-编辑", notes="浏览记录信息表-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody SysViewRecord sysViewRecord) {
		sysViewRecordService.updateById(sysViewRecord);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "浏览记录信息表-通过id删除")
	@ApiOperation(value="浏览记录信息表-通过id删除", notes="浏览记录信息表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		sysViewRecordService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "浏览记录信息表-批量删除")
	@ApiOperation(value="浏览记录信息表-批量删除", notes="浏览记录信息表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.sysViewRecordService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "浏览记录信息表-通过id查询")
	@ApiOperation(value="浏览记录信息表-通过id查询", notes="浏览记录信息表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<SysViewRecord> queryById(@RequestParam(name="id",required=true) String id) {
		SysViewRecord sysViewRecord = sysViewRecordService.getById(id);
		if(sysViewRecord==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(sysViewRecord);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param sysViewRecord
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SysViewRecord sysViewRecord) {
        return super.exportXls(request, sysViewRecord, SysViewRecord.class, "浏览记录信息表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SysViewRecord.class);
    }

}
